package com.wwwarehouse.taskcenter.custom_view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wwwarehouse.taskcenter.R;

import static fm.jiecao.jcvideoplayer_lib.JCUtils.dip2px;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/5 8:52
 * @描述 ${TODO}
 */

public class HorizontalNumView extends View {

    private Paint mPaintc1;
    private Paint mTextPaint;
    private Rect mTextBound = new Rect();

    public HorizontalNumView(Context context) {
        super(context);
        init(context);
    }

    public HorizontalNumView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HorizontalNumView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private Context mContext;

    private void init(Context context) {
        mContext = context;
        mTextPaint = new Paint();
        mTextPaint.setTextSize(mContext.getResources().getDimension(R.dimen.common_dimen_dp15));
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(context.getResources().getColor(R.color.common_color_c5_45494e));
    }

    private int currentHeight = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mTotal != 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(0, 0, (((getWidth() - dp2px(40)) * currentHeight / mTotal)), dp2px(30), dp2px(3), dp2px(3), mPaintc1);
            } else {
                canvas.drawRect(0, 0, (((getWidth() - dp2px(40)) * currentHeight / mTotal)), dp2px(30), mPaintc1);
            }
            mTextPaint.getTextBounds(currentHeight + "", 0, (currentHeight + "").length(), mTextBound);
            //        canvas.drawText(currentHeight + "", ((getWidth()-dp2px(40)) * currentHeight / mTotal)+dp2px(8), getMeasuredHeight() - mTextBound.height() + 6, mTextPaint);
            if (currentHeight != 0) {
                canvas.drawText(currentHeight + "", ((getWidth() - dp2px(40)) * currentHeight / mTotal) + dp2px(8), getMeasuredHeight() - mTextBound.height() + 6, mTextPaint);
            }
            //        LogUtils.showInfoLog("getMeasuredHeight()/2" + getMeasuredHeight() / 2 + "mTextBound" + mTextBound);
            if (currentHeight < mHeight) {
                currentHeight += 1;
                invalidate();
            }

        }

    }

    private int dp2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private int mHeight;
    private int mTotal;
    private int mColor;

    public void setData(int heigh, int total, int color) {
        currentHeight = 0;
        mHeight = heigh;
        mTotal = total;
        mColor = color;
        mPaintc1 = new Paint();
        mPaintc1.setColor(mColor);
        mPaintc1.setAntiAlias(true);
        mPaintc1.setStyle(Paint.Style.FILL);
        mPaintc1.setStrokeWidth(dip2px(mContext, 80));
        invalidate();
    }
}
